package com.shsxt.service;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alipay.api.AlipayApiException;
import com.shsxt.controller.YunTongXun;
import com.shsxt.dao.AuntDao;
import com.shsxt.dao.CouponDao;
import com.shsxt.dao.OrderDao;
import com.shsxt.dao.WodeDao;
import com.shsxt.model.BOrder;
import com.shsxt.model.COrder;
import com.shsxt.model.Coupon;
import com.shsxt.model.DOrder;
import com.shsxt.model.JOrder;
import com.shsxt.model.KOrder;
import com.shsxt.model.ROrder;
import com.shsxt.model.SOrder;
import com.shsxt.model.ServicePersonal;
import com.shsxt.model.YOrder;
import com.shsxt.vo.AuntOrderQuery;
import com.shsxt.vo.EvaluationScore;
import com.shsxt.vo.OrderCircle;
import com.shsxt.vo.OrderQuery;

@Service
public class OrderService {
	
	@Autowired
	private CouponDao couponDao;
	@Autowired
	private OrderDao orderDao;
	@Autowired
	private WodeDao wodeDao;
	@Autowired
	private YuyueService yuyueService;
	@Autowired
	private AuntDao auntDao;
	@Autowired
	private WodeService wodeService;
	/**
	 * 获取营销机会下的计划项
	 * @param saleChanceId
	 * @return
	 * @throws ParseException 
	 */
	//--------------------------------------------------------------------------------------------
	  //订单未完成(该用户未完成的订单)
	public Map<String, Object> findUndoneOrder(String phoneNumber) throws ParseException {
		Map<String, Object> map = new HashMap<String,Object>();
		if(phoneNumber==null||phoneNumber.equals("")){
			map.put("undone",new ArrayList());
			return map;
		}
		List<OrderQuery> OrderQuery2=new ArrayList<OrderQuery>();
		List<OrderQuery> OrderQuery=orderDao.findUndoneOrder(phoneNumber);
		Map<String, Object> param1 = new HashMap<String,Object>();
		param1.put("phoneNumber", phoneNumber);
		param1.put("servicePersonal", "");
		List<OrderCircle> list=orderDao.findUndoneCircle(param1);
		OrderQuery oq=null;
		for(OrderCircle oc:list){
			String[] days=oc.getCircle().split(";");
			String[] day=days[0].split(",");
			String[] slot=days[1].split(",");
			SimpleDateFormat to=new SimpleDateFormat("yyyy-MM-dd EEEE");
			SimpleDateFormat from=new SimpleDateFormat("yyyy年MM月dd日");
			Date date=from.parse(day[0]);
			String da=to.format(date);
			oq=new OrderQuery(oc.getOrderNumber(),oc.getType()+"("+days[2]+")",da,oc.getAddress(),oc.getDetailAddress(),oc.getOrderState(),slot[0]+"("+days[2]+")");
			OrderQuery.add(oq);
		}
		for(OrderQuery oq2:OrderQuery){
			if(oq2.getOrderState().equals("待支付")){
				OrderQuery2.add(oq2);
			}
		}
		for(OrderQuery oq2:OrderQuery){
			if(oq2.getOrderState().equals("待接单")){
				OrderQuery2.add(oq2);
			}
		}
		for(OrderQuery oq2:OrderQuery){
			if(oq2.getOrderState().equals("已接单")){
				OrderQuery2.add(oq2);
			}
		}
		for(OrderQuery oq2:OrderQuery){
			if(oq2.getOrderState().equals("待面试")){
				OrderQuery2.add(oq2);
			}
		}
		map.put("undone",paixuOrderQuery(OrderQuery2));
		return map;
	}
	  //订单已完成(该用户已完成的订单)
	public Map<String, Object> findDoneOrder(String phoneNumber) throws ParseException {
		Map<String, Object> map = new HashMap<String,Object>();
		if(phoneNumber==null||phoneNumber.equals("")){
			map.put("done",new ArrayList());
			return map;
		}
		//已完成已评价   已完成未评价  
		List<OrderQuery> doneScore=orderDao.findDoneScore(phoneNumber);
		doneScore.addAll(orderDao.findDoneNoScore(phoneNumber));
		//已完成或待评价的面试订单集合
		Map<String, Object> param1 = new HashMap<String,Object>();
		param1.put("phoneNumber", phoneNumber);
		param1.put("servicePersonal", "");
		List<OrderCircle> list=orderDao.findDoneScoreCircle(param1);
		list.addAll(orderDao.findDoneUnscoreCircle(param1));
		
		OrderQuery oq=null;
		//遍历(已完成或待评价的面试订单集合)加入到已完成里面
		for(OrderCircle oc:list){
			String[] days=oc.getCircle().split(";");
			String[] day=days[0].split(",");
			String[] slot=days[1].split(",");
			SimpleDateFormat to=new SimpleDateFormat("yyyy-MM-dd EEEE");
			SimpleDateFormat from=new SimpleDateFormat("yyyy年MM月dd日");
			Date date=from.parse(day[0]);
			String da=to.format(date);
			oq=new OrderQuery(oc.getOrderNumber(),oc.getType()+"("+days[2]+")",da,oc.getAddress(),oc.getDetailAddress(),oc.getOrderState(),slot[0]+"("+days[2]+")");
			doneScore.add(oq);
		}
		map.put("done",paixuOrderQuery(doneScore));
		return map;
	}
	  //订单已完成待评价
	public Map<String, Object> findUnscore(String phoneNumber) throws ParseException {
		Map<String, Object> map = new HashMap<String,Object>();
		if(phoneNumber==null||phoneNumber.equals("")){
			map.put("done_unscore",new ArrayList());
			return map;
		}
		List<OrderQuery> doneNoScore=orderDao.findDoneNoScore(phoneNumber);
		Map<String, Object> param1 = new HashMap<String,Object>();
		param1.put("phoneNumber", phoneNumber);
		param1.put("servicePersonal", "");
		//待评价的周期订单集合
			List<OrderCircle> list=orderDao.findDoneUnscoreCircle(param1);
			
			OrderQuery oq=null;
			//遍历(待评价的周期订单集合)加入到待评价里面
			for(OrderCircle oc:list){
				String[] days=oc.getCircle().split(";");
				String[] day=days[0].split(",");
				String[] slot=days[1].split(",");
				SimpleDateFormat to=new SimpleDateFormat("yyyy-MM-dd EEEE");
				SimpleDateFormat from=new SimpleDateFormat("yyyy年MM月dd日");
				Date date=from.parse(day[0]);
				String da=to.format(date);
				oq=new OrderQuery(oc.getOrderNumber(),oc.getType()+"("+days[2]+")",da,oc.getAddress(),oc.getDetailAddress(),oc.getOrderState(),slot[0]+"("+days[2]+")");
				doneNoScore.add(oq);
			}
		map.put("done_unscore",paixuOrderQuery(doneNoScore));
		return map;
	}
	public Map<String, Object> findAllOrder(String phoneNumber) throws ParseException {
		Map<String, Object> map = new HashMap<String,Object>();
		if(phoneNumber==null||phoneNumber.equals("")){
			map.put("allorder",new ArrayList());
			return map;
		}
		List<OrderQuery> list=orderDao.findUndoneOrder(phoneNumber);
		List<OrderQuery> doneScore=orderDao.findDoneScore(phoneNumber);
		List<OrderQuery> doneNoScore=orderDao.findDoneNoScore(phoneNumber);
		List<OrderQuery> cancel=orderDao.findCancelOrder(phoneNumber);
		list.addAll(doneScore);
		list.addAll(doneNoScore);
		list.addAll(cancel);
		//周期订单集合
		Map<String, Object> param1 = new HashMap<String,Object>();
		param1.put("phoneNumber", phoneNumber);
		param1.put("servicePersonal", "");


				List<OrderCircle> ocs=orderDao.findDoneScoreCircle(param1);

				ocs.addAll(orderDao.findDoneUnscoreCircle(param1));
				ocs.addAll(orderDao.findUndoneCircle(param1));
				OrderQuery oq=null;
				//遍历(已完成或待评价的面试订单集合)加入到已完成里面
				for(OrderCircle oc:ocs){
					String[] days=oc.getCircle().split(";");
					String[] day=days[0].split(",");
					String[] slot=days[1].split(",");
					SimpleDateFormat to=new SimpleDateFormat("yyyy-MM-dd EEEE");
					SimpleDateFormat from=new SimpleDateFormat("yyyy年MM月dd日");
					Date date=from.parse(day[0]);
					String da=to.format(date);
					oq=new OrderQuery(oc.getOrderNumber(),oc.getType()+"("+days[2]+")",da,oc.getAddress(),oc.getDetailAddress(),oc.getOrderState(),slot[0]+"("+days[2]+")");
					list.add(oq);
				}
		map.put("allorder",paixuOrderQuery(list));
		return map;
	}
    //待支付订单详情(订单状态服务时间服务地点联系电话保洁时间和总价清洁剂时间和总价订单总价)
	public List<Object> findOrderMessage(String orderNumber,String type) throws ParseException {
		List<Object> orderDetails=new ArrayList<Object>();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd EEEE");
		 SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
			if(type.equals("日常保洁")){
				ROrder r= orderDao.findROrder(orderNumber);
				if(r.getOrderTime()!=null){
					String state=r.getOrderState();
					orderDetails.add(r.getOrderState());
				//	orderDetails.add(DateFormat.getDateInstance(DateFormat.FULL).format(format.parse(r.getOrderTime()))+r.getTimeSlot());
					
					orderDetails.add(  format.format(ft.parse(r.getOrderTime()))+r.getTimeSlot());
					orderDetails.add(r.getAddress());
					orderDetails.add(r.getDetailAddress());
					orderDetails.add(r.getPhoneNumber());
					orderDetails.add(r.getOrderNumber());
					orderDetails.add(r.getServiceTime()+"小时");
					orderDetails.add(r.getPrice()-r.getCleaningSupplies_Price()+"元");
					orderDetails.add("5.0元/小时 *"+ r.getServiceTime()+"小时");
					orderDetails.add(r.getCleaningSupplies_Price()+"元");
					orderDetails.add(r.getRealPrice()+"元");
					if(state.equals("已完成")||state.equals("已评价")){
						 orderDetails.addAll(findSP(r.getServicePersonal()));
					}
					if(state.equals("待支付")){
						orderDetails.add(findDaojishi(orderNumber, type));
					}
				}else{
					String state=r.getOrderState();
					orderDetails.add(r.getOrderState());
				//	orderDetails.add(DateFormat.getDateInstance(DateFormat.FULL).format(format.parse(r.getOrderTime()))+r.getTimeSlot());
					SimpleDateFormat www=new SimpleDateFormat("EEEE");
					SimpleDateFormat to=new SimpleDateFormat("yyyy-MM-dd EEEE");
					SimpleDateFormat from=new SimpleDateFormat("yyyy年MM月dd日");
					String[] days=r.getCircle().split(";");
					String[] day=days[0].split(",");
					String[] slot=days[1].split(",");
					String week=days[2];
					if(week.equals("两周一次")||week.equals("一周一次")){
						String a=www.format(from.parse(days[0]));
						String b=slot[0];
						orderDetails.add(a+b+"("+week+")");
						
						String c=to.format(from.parse(days[0]))+"开始服务";
						orderDetails.add(c);
					}
					if(week.equals("一周多次")){
						String times="";
						int a=day.length/Integer.valueOf(days[3]);
						for(int x=0;x<a;x++){
							String str=www.format(from.parse(day[x]));
							times=times+str+slot[x]+";";
						}
						orderDetails.add(times);
						
						String c=to.format(from.parse(day[0]))+"开始服务";
						orderDetails.add(c);
					}
					
					
					
					
					
					
					orderDetails.add(r.getAddress());
					orderDetails.add(r.getDetailAddress());
					orderDetails.add(r.getPhoneNumber());
					orderDetails.add(r.getOrderNumber());
					orderDetails.add(r.getServiceTime()+"小时");
					orderDetails.add((r.getPrice()-r.getCleaningSupplies_Price())/Float.valueOf(days[3])+"元");
					if(r.getCleaningSupplies_Price()==0){
						orderDetails.add("");
					}else{
						orderDetails.add("5.0元/小时 *"+ r.getServiceTime()+"小时");
					}
					orderDetails.add(r.getCleaningSupplies_Price()/Float.valueOf(days[3])+"元");
					if(week.equals("两周一次")){
						orderDetails.add(Integer.valueOf(days[3])+"次");
					}else{
						orderDetails.add(days[3]+"周");

					}
					orderDetails.add(r.getRealPrice()+"元");
					if(state.equals("已完成")||state.equals("已评价")){
						 orderDetails.addAll(findSP(r.getServicePersonal()));
					}
					if(state.equals("待支付")){
						orderDetails.add(findDaojishi(orderNumber, type));
					}
				}
				
			}else if(type.equals("沙发护理")){
				SOrder r= orderDao.findSOrder(orderNumber);
				String state=r.getOrderState();
				orderDetails.add(r.getOrderState());
				orderDetails.add(  format.format(ft.parse(r.getOrderTime()))+r.getTimeSlot());
				orderDetails.add(r.getAddress());
				orderDetails.add(r.getDetailAddress());
				orderDetails.add(r.getPhoneNumber());
				orderDetails.add(r.getOrderNumber());
				orderDetails.add(r.getTypeDetail());
				orderDetails.add(r.getRealPrice()+"元");
				if(state.equals("已完成")||state.equals("已评价")){
					 orderDetails.addAll(findSP(r.getServicePersonal()));
					 
				}
				if(state.equals("待支付")){
					orderDetails.add(findDaojishi(orderNumber, type));
				}
			}else if(type.equals("开荒保洁")){
				KOrder r= orderDao.findKOrder(orderNumber);
				String state=r.getOrderState();
				orderDetails.add(r.getOrderState());
				orderDetails.add(  format.format(ft.parse(r.getOrderTime()))+r.getTimeSlot());
				orderDetails.add(r.getAddress());
				orderDetails.add(r.getDetailAddress());
				orderDetails.add(r.getPhoneNumber());
				orderDetails.add(r.getOrderNumber());
				orderDetails.add(r.getTypeDetail());
				orderDetails.add(r.getRealPrice()+"元");
				if(state.equals("已完成")||state.equals("已评价")){
					 orderDetails.addAll(findSP(r.getServicePersonal()));
					 
				}
				if(state.equals("待支付")){
					orderDetails.add(findDaojishi(orderNumber, type));
				}
			}else if(type.equals("家电清洗")){
				JOrder r= orderDao.findJOrder(orderNumber);
				String state=r.getOrderState();
				orderDetails.add(r.getOrderState());
				orderDetails.add(  format.format(ft.parse(r.getOrderTime()))+r.getTimeSlot());
				orderDetails.add(r.getAddress());
				orderDetails.add(r.getDetailAddress());
				orderDetails.add(r.getPhoneNumber());
				orderDetails.add(r.getOrderNumber());
				orderDetails.add(r.getTypeDetail());
				orderDetails.add(r.getRealPrice()+"元");
				if(state.equals("已完成")||state.equals("已评价")){
					 orderDetails.addAll(findSP(r.getServicePersonal()));
					 
				}
				if(state.equals("待支付")){
					orderDetails.add(findDaojishi(orderNumber, type));
				}
			}else if(type.equals("地板护理")){
				DOrder r= orderDao.findDOrder(orderNumber);
				String state=r.getOrderState();
				orderDetails.add(r.getOrderState());
				orderDetails.add(  format.format(ft.parse(r.getOrderTime()))+r.getTimeSlot());
				orderDetails.add(r.getAddress());
				orderDetails.add(r.getDetailAddress());
				orderDetails.add(r.getPhoneNumber());
				orderDetails.add(r.getOrderNumber());
				orderDetails.add(r.getTypeDetail());
				orderDetails.add(r.getRealPrice()+"元");
				if(state.equals("已完成")||state.equals("已评价")){
					 orderDetails.addAll(findSP(r.getServicePersonal()));
					
				}
				if(state.equals("待支付")){
					orderDetails.add(findDaojishi(orderNumber, type));
				}
			}else if(type.equals("擦玻璃")){
				COrder r= orderDao.findCOrder(orderNumber);
				String state=r.getOrderState();
				orderDetails.add(r.getOrderState());
				orderDetails.add(  format.format(ft.parse(r.getOrderTime()))+r.getTimeSlot());
				orderDetails.add(r.getAddress());
				orderDetails.add(r.getDetailAddress());
				orderDetails.add(r.getPhoneNumber());
				orderDetails.add(r.getOrderNumber());
				orderDetails.add(r.getTypeDetail());
				orderDetails.add(r.getRealPrice()+"元");
				if(state.equals("已完成")||state.equals("已评价")){

					 orderDetails.addAll(findSP(r.getServicePersonal()));

				}
				if(state.equals("待支付")){
					orderDetails.add(findDaojishi(orderNumber, type));
				}
			}else if(type.equals("月嫂")){
				YOrder r= orderDao.findYOrder(orderNumber);
				orderDetails.add(r.getOrderState());
				orderDetails.add("月嫂");
				
				orderDetails.add(  format.format(ft.parse(r.getOrderTime())));
				orderDetails.add(r.getAddress());
				orderDetails.add(r.getDetailAddress());
				orderDetails.add(r.getPhoneNumber());
				orderDetails.add(r.getOrderNumber());
				
			}else if(type.equals("保姆")){
				BOrder r= orderDao.findBOrder(orderNumber);
				orderDetails.add(r.getOrderState());
				orderDetails.add("保姆");
				orderDetails.add(  format.format(ft.parse(r.getOrderTime())));
				orderDetails.add(r.getAddress());
				orderDetails.add(r.getPhoneNumber());
				orderDetails.add(r.getOrderNumber());
				
			}
			
		
		return orderDetails;
	}
    //去评价
	public  synchronized  Map<String, Object> updateEvaluate(String orderNumber, String type,String evaluation,String score) {
		Map<String, Object> param = new HashMap<String,Object>();
		Map<String, Object> map = new HashMap<String,Object>();
		param.put("orderNumber",orderNumber);
		param.put("type",type);
		param.put("evaluation",evaluation);
		param.put("score", score+"0%");
		String servicePerosnal="";
		if(type.equals("日常保洁")||type.equals("日常保洁(一周一次)")||type.equals("日常保洁(一周多次)")||type.equals("日常保洁(两周一次)")){
			orderDao.updateREvaluate(param);
			ROrder r=orderDao.findROrder(orderNumber);
			servicePerosnal=r.getServicePersonal();
		}else if(type.equals("沙发护理")){
			orderDao.updateSEvaluate(param);
			SOrder r=orderDao.findSOrder(orderNumber);
			servicePerosnal=r.getServicePersonal();
		}else if(type.equals("开荒保洁")){
			orderDao.updateKEvaluate(param);
			KOrder r=orderDao.findKOrder(orderNumber);
			servicePerosnal=r.getServicePersonal();
		}else if(type.equals("家电清洗")){
			orderDao.updateJEvaluate(param);
			JOrder r=orderDao.findJOrder(orderNumber);
			servicePerosnal=r.getServicePersonal();
		}else if(type.equals("地板护理")){
			orderDao.updateDEvaluate(param);
			DOrder r=orderDao.findDOrder(orderNumber);
			servicePerosnal=r.getServicePersonal();
		}else if(type.equals("擦玻璃")){
			orderDao.updateCEvaluate(param);
			COrder r=orderDao.findCOrder(orderNumber);
			servicePerosnal=r.getServicePersonal();
		}
		map.put("0","成功");
		List<EvaluationScore> aqs=auntDao.findEvaluationOrder(servicePerosnal);
		int count=aqs.size();
		int scores=0;
		for(EvaluationScore es:aqs){
			int a=Integer.valueOf(es.getScore().substring(0, 1));
			scores+=a;
		}
		int scoreEnd=scores/count;
		

		param.put("servicePerosnal", servicePerosnal);
		param.put("scoreEnd", scoreEnd+"0%");
		auntDao.updateScoreServicePersonal(param);
		return map;
	}
	//查看评价
	public Map<String, Object> findEvaluate(String orderNumber, String type) {
		Map<String, Object> map = new HashMap<String,Object>();

		if(type.equals("日常保洁")){
			ROrder r=orderDao.findROrder(orderNumber);
			map.put("0", r.getPhoneNumber());
			map.put("1", r.getScore());
			map.put("2", r.getEvaluation());
			map.put("3", r.getEvaluationTime());
		}else if(type.equals("沙发护理")){
			SOrder r=orderDao.findSOrder(orderNumber);
			map.put("0", r.getPhoneNumber());
			map.put("1", r.getScore());
			map.put("2", r.getEvaluation());
			map.put("3", r.getEvaluationTime());
		}else if(type.equals("开荒保洁")){
			KOrder r=orderDao.findKOrder(orderNumber);
			map.put("0", r.getPhoneNumber());
			map.put("1", r.getScore());
			map.put("2", r.getEvaluation());
			map.put("3", r.getEvaluationTime());
		}else if(type.equals("家电清洗")){
			JOrder r=orderDao.findJOrder(orderNumber);
			map.put("0", r.getPhoneNumber());
			map.put("1", r.getScore());
			map.put("2", r.getEvaluation());
			map.put("3", r.getEvaluationTime());
		}else if(type.equals("地板护理")){
			DOrder r=orderDao.findDOrder(orderNumber);
			map.put("0", r.getPhoneNumber());
			map.put("1", r.getScore());
			map.put("2", r.getEvaluation());
			map.put("3", r.getEvaluationTime());
		}else if(type.equals("擦玻璃")){
			COrder r=orderDao.findCOrder(orderNumber);
			map.put("0", r.getPhoneNumber());
			map.put("1", r.getScore());
			map.put("2", r.getEvaluation());
			map.put("3", r.getEvaluationTime());
		}else if(type.equals("月嫂")){
			YOrder r=orderDao.findYOrder(orderNumber);
			map.put("0", r.getPhoneNumber());
			map.put("1", r.getScore());
			map.put("2", r.getEvaluation());
			map.put("3", r.getEvaluationTime());
		}else if(type.equals("保姆")){
			BOrder r=orderDao.findBOrder(orderNumber);
			map.put("0", r.getPhoneNumber());
			map.put("1", r.getScore());
			map.put("2", r.getEvaluation());
			map.put("3", r.getEvaluationTime());
		}
		return map;
	}
    //取消订单
	public  synchronized Map<String, Object> cancelOrder(HttpServletRequest request,HttpServletResponse response,String orderNumber, String type) throws Exception {
		Map<String, Object> map = new HashMap<String,Object>();
		Map<String, Object> param = new HashMap<String,Object>();
		int l=-1;
			if(type.equals("日常保洁")||type.equals("日常保洁(一周多次)")||type.equals("日常保洁(一周一次)")||type.equals("日常保洁(两周一次)")){
				//如果是取消已支付的订单  实现退款
				ROrder r=orderDao.findROrder(orderNumber);
				if(r.getOrderState().equals("待接单")){
					if(r.getPayType().equals("支付宝")){
						yuyueService.alipayRefundRequest(orderNumber,r.getRealPrice()+"");
					}else if(r.getPayType().substring(0, 2).equals("微信")){
						yuyueService.wxtuikuan(request,response,orderNumber,(int) (Float.valueOf(r.getRealPrice())*100f)+"");
					}else if(r.getPayType().equals("余额")){
						param.put("phoneNumber", r.getPhoneNumber());
						param.put("price", "-"+r.getRealPrice());
						String vip=wodeDao.findUserByPhone(r.getPhoneNumber()).getMemberLevel();
						if(vip.equals("VIP1")||vip.equals("VIP2")){
							param.put("price","-"+(Float)(r.getRealPrice())*(0.75f));
						}else if(vip.equals("VIP3")||vip.equals("VIP4")){
							param.put("price","-"+(Float)r.getRealPrice()*(0.7f));
						}else if(vip.equals("VIP5")||vip.equals("VIP6")){
							param.put("price","-"+(Float)r.getRealPrice()*(0.65f));
						}
						
						wodeDao.updateMemberBalance(param);
					}
					System.err.println("成功"+r.getRedeemCode());
					if(r.getRedeemCode()!=null&&!r.getRedeemCode().equals("")){
						System.err.println("成功");
						couponDao.resetCoupon(r.getRedeemCode());
					}
					if(r.getOrderTime()==null||r.getOrderTime().equals("")){
							int start=0;
							int end=0;
							String circle=r.getCircle();
							String[] days=circle.split(";")[0].split(",");
							String[] slots=circle.split(";")[1].split(",");
							for(int i=0;i<days.length;i++){
								if(slots[i].substring(0, 1).equals("8")||slots[i].substring(0, 1).equals("9")){
									start=Integer.valueOf(slots[i].substring(0, 1));
								}else{
									start=Integer.valueOf(slots[i].substring(0, 2));
								}
								end=Integer.valueOf(slots[i].substring(slots[i].length()-5, slots[i].length()-3));
								yuyueService.updateYueMan(end-start+"",slots[i],days[i],-1);
							}
						}else{
							yuyueService.updateYueMan(r.getServiceTime()+"", r.getTimeSlot(),new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse(r.getOrderTime())),-1);
						}
				}
				l=orderDao.cancelROrder(orderNumber);
			}else if(type.equals("沙发护理")){
				//如果是取消已支付的订单  实现退款
				SOrder r=orderDao.findSOrder(orderNumber);
				if(r.getOrderState().equals("待接单")){
					if(r.getPayType().equals("支付宝")){
						yuyueService.alipayRefundRequest(orderNumber,r.getRealPrice()+"");
					}else if(r.getPayType().substring(0, 2).equals("微信")){
						yuyueService.wxtuikuan(request,response,orderNumber,(int) (Float.valueOf(r.getRealPrice())*100f)+"");
					}else if(r.getPayType().equals("余额")){
						param.put("phoneNumber", r.getPhoneNumber());
						param.put("price", "-"+r.getRealPrice());
						String vip=wodeDao.findUserByPhone(r.getPhoneNumber()).getMemberLevel();
						if(vip.equals("VIP1")||vip.equals("VIP2")){
							param.put("price","-"+(Float)(r.getRealPrice())*(0.75f));
						}else if(vip.equals("VIP3")||vip.equals("VIP4")){
							param.put("price","-"+(Float)r.getRealPrice()*(0.7f));
						}else if(vip.equals("VIP5")||vip.equals("VIP6")){
							param.put("price","-"+(Float)r.getRealPrice()*(0.65f));
						}
						wodeDao.updateMemberBalance(param);
					}
					if(r.getRedeemCode()!=null&&!r.getRedeemCode().equals("")){
						couponDao.resetCoupon(r.getRedeemCode());
					}
						yuyueService.updateYueMan("4", r.getTimeSlot(),new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse(r.getOrderTime())),-1);

				}
				l=orderDao.cancelSOrder(orderNumber);
			}else if(type.equals("开荒保洁")){
				//如果是取消已支付的订单  实现退款
				KOrder r=orderDao.findKOrder(orderNumber);
				if(r.getOrderState().equals("待接单")){
					if(r.getPayType().equals("支付宝")){
						yuyueService.alipayRefundRequest(orderNumber,r.getRealPrice()+"");
					}else if(r.getPayType().substring(0, 2).equals("微信")){
						yuyueService.wxtuikuan(request,response,orderNumber,(int) (Float.valueOf(r.getRealPrice())*100f)+"");
					}else if(r.getPayType().equals("余额")){
						param.put("phoneNumber", r.getPhoneNumber());
						param.put("price", "-"+r.getRealPrice());
						String vip=wodeDao.findUserByPhone(r.getPhoneNumber()).getMemberLevel();
						if(vip.equals("VIP1")||vip.equals("VIP2")){
							param.put("price","-"+(Float)(r.getRealPrice())*(0.75f));
						}else if(vip.equals("VIP3")||vip.equals("VIP4")){
							param.put("price","-"+(Float)r.getRealPrice()*(0.7f));
						}else if(vip.equals("VIP5")||vip.equals("VIP6")){
							param.put("price","-"+(Float)r.getRealPrice()*(0.65f));
						}
						wodeDao.updateMemberBalance(param);
					}
					if(r.getRedeemCode()!=null&&!r.getRedeemCode().equals("")){
						couponDao.resetCoupon(r.getRedeemCode());
					}
						yuyueService.updateYueMan("4", r.getTimeSlot(),new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse(r.getOrderTime())),-1);

				}
				l=orderDao.cancelKOrder(orderNumber);
			}else if(type.equals("家电清洗")){
				//如果是取消已支付的订单  实现退款
				JOrder r=orderDao.findJOrder(orderNumber);
				if(r.getOrderState().equals("待接单")){
					
					if(r.getPayType().equals("支付宝")){
						yuyueService.alipayRefundRequest(orderNumber,r.getRealPrice()+"");
					}else if(r.getPayType().substring(0, 2).equals("微信")){
						yuyueService.wxtuikuan(request,response,orderNumber,(int) (Float.valueOf(r.getRealPrice())*100f)+"");
					}else if(r.getPayType().equals("余额")){
						param.put("phoneNumber", r.getPhoneNumber());
						param.put("price", "-"+r.getRealPrice());
						String vip=wodeDao.findUserByPhone(r.getPhoneNumber()).getMemberLevel();
						if(vip.equals("VIP1")||vip.equals("VIP2")){
							param.put("price","-"+(Float)(r.getRealPrice())*(0.75f));
						}else if(vip.equals("VIP3")||vip.equals("VIP4")){
							param.put("price","-"+(Float)r.getRealPrice()*(0.7f));
						}else if(vip.equals("VIP5")||vip.equals("VIP6")){
							param.put("price","-"+(Float)r.getRealPrice()*(0.65f));
						}
						wodeDao.updateMemberBalance(param);
					}
					if(r.getRedeemCode()!=null&&!r.getRedeemCode().equals("")){
						couponDao.resetCoupon(r.getRedeemCode());
					}
						yuyueService.updateYueMan("4", r.getTimeSlot(),new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse(r.getOrderTime())),-1);

				}
				l=orderDao.cancelJOrder(orderNumber);
			}else if(type.equals("地板护理")){
				//如果是取消已支付的订单  实现退款
				DOrder r=orderDao.findDOrder(orderNumber);
				if(r.getOrderState().equals("待接单")){
					if(r.getPayType().equals("支付宝")){
						yuyueService.alipayRefundRequest(orderNumber,r.getRealPrice()+"");
					}else if(r.getPayType().substring(0, 2).equals("微信")){
						yuyueService.wxtuikuan(request,response,orderNumber,(int) (Float.valueOf(r.getRealPrice())*100f)+"");
					}else if(r.getPayType().equals("余额")){
						param.put("phoneNumber", r.getPhoneNumber());
						param.put("price", "-"+r.getRealPrice());
						String vip=wodeDao.findUserByPhone(r.getPhoneNumber()).getMemberLevel();
						if(vip.equals("VIP1")||vip.equals("VIP2")){
							param.put("price","-"+(Float)(r.getRealPrice())*(0.75f));
						}else if(vip.equals("VIP3")||vip.equals("VIP4")){
							param.put("price","-"+(Float)r.getRealPrice()*(0.7f));
						}else if(vip.equals("VIP5")||vip.equals("VIP6")){
							param.put("price","-"+(Float)r.getRealPrice()*(0.65f));
						}
						wodeDao.updateMemberBalance(param);
					}
					if(r.getRedeemCode()!=null&&!r.getRedeemCode().equals("")){
						couponDao.resetCoupon(r.getRedeemCode());
					}
						yuyueService.updateYueMan("4", r.getTimeSlot(),new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse(r.getOrderTime())),-1);

				}
				l=orderDao.cancelDOrder(orderNumber);
			}else if(type.equals("擦玻璃")){
				//如果是取消已支付的订单  实现退款
				COrder r=orderDao.findCOrder(orderNumber);
				if(r.getOrderState().equals("待接单")){
					if(r.getPayType().equals("支付宝")){
						yuyueService.alipayRefundRequest(orderNumber,r.getRealPrice()+"");
					}else if(r.getPayType().substring(0, 2).equals("微信")){
						yuyueService.wxtuikuan(request,response,orderNumber,(int) (Float.valueOf(r.getRealPrice())*100f)+"");
					}else if(r.getPayType().equals("余额")){
						param.put("phoneNumber", r.getPhoneNumber());
						param.put("price", "-"+r.getRealPrice());
						String vip=wodeDao.findUserByPhone(r.getPhoneNumber()).getMemberLevel();
						if(vip.equals("VIP1")||vip.equals("VIP2")){
							param.put("price","-"+(Float)(r.getRealPrice())*(0.75f));
						}else if(vip.equals("VIP3")||vip.equals("VIP4")){
							param.put("price","-"+(Float)r.getRealPrice()*(0.7f));
						}else if(vip.equals("VIP5")||vip.equals("VIP6")){
							param.put("price","-"+(Float)r.getRealPrice()*(0.65f));
						}
						wodeDao.updateMemberBalance(param);
					}
					if(r.getRedeemCode()!=null&&!r.getRedeemCode().equals("")){
						couponDao.resetCoupon(r.getRedeemCode());
					}
						yuyueService.updateYueMan("4", r.getTimeSlot(),new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse(r.getOrderTime())),-1);

				}
				l=orderDao.cancelCOrder(orderNumber);
			}else if(type.equals("月嫂")){
				l=orderDao.cancelYOrder(orderNumber);
			}else if(type.equals("保姆")){
				l=orderDao.cancelBOrder(orderNumber);
			}
			
			if(l!=-1){
				map.put("0", "成功");
			}else{
				map.put("0", "失败");
			}



			
			return map;
		}
	//删除订单
	public  synchronized  Map<String, Object> deleteOrder(String orderNumber, String type) {
		Map<String, Object> map = new HashMap<String,Object>();
		int l=-1;
			if(type.equals("日常保洁")||type.equals("日常保洁(一周多次)")||type.equals("日常保洁(一周一次)")||type.equals("日常保洁(两周一次)")){
				l=orderDao.deleteROrder(orderNumber);
			}else if(type.equals("沙发护理")){
				l=orderDao.deleteSOrder(orderNumber);
			}else if(type.equals("开荒保洁")){
				l=orderDao.deleteKOrder(orderNumber);
			}else if(type.equals("家电清洗")){
				l=orderDao.deleteJOrder(orderNumber);
			}else if(type.equals("地板护理")){
				l=orderDao.deleteDOrder(orderNumber);
			}else if(type.equals("擦玻璃")){
				l=orderDao.deleteCOrder(orderNumber);
			}else if(type.equals("月嫂")){
				l=orderDao.deleteYOrder(orderNumber);
			}else if(type.equals("保姆")){
				l=orderDao.deleteBOrder(orderNumber);
			}
			
			if(l!=-1){
				map.put("0", "成功");
			}else{
				map.put("0", "失败");
			}



			
			return map;
	}
	
	
//-------------------------------------------------------------------------
//找订单对应的阿姨
	public List<String> findSP(String phoneNumber) {
		List<String> list=new ArrayList<String>(); 
		ServicePersonal sp=orderDao.findPersonal(phoneNumber);
		list.add(sp.getName());
		list.add(sp.getHoneLocation());
		list.add(sp.getAge());
		list.add(sp.getServiceTimes());
		list.add(sp.getIdcard());
		list.add(sp.getScore());
		list.add(sp.getHeadProtrait());
		return list;
	
	}
	
	public Map<String, Object> toEvaluate(String orderNumber, String type) {
		Map<String, Object> map = new HashMap<String,Object>();
		List<String> list=new ArrayList<String>();
			if(type.equals("日常保洁")){
				ROrder r=orderDao.findROrder(orderNumber);
				list=findSP(r.getServicePersonal());
			}else if(type.equals("沙发护理")){
				SOrder r=orderDao.findSOrder(orderNumber);
				list=findSP(r.getServicePersonal());
			}else if(type.equals("开荒保洁")){
				KOrder r=orderDao.findKOrder(orderNumber);
				list=findSP(r.getServicePersonal());
			}else if(type.equals("家电清洗")){
				JOrder r=orderDao.findJOrder(orderNumber);
				list=findSP(r.getServicePersonal());
			}else if(type.equals("地板护理")){
				DOrder r=orderDao.findDOrder(orderNumber);
				list=findSP(r.getServicePersonal());
			}else if(type.equals("擦玻璃")){
				COrder r=orderDao.findCOrder(orderNumber);
				list=findSP(r.getServicePersonal());
			}else if(type.equals("月嫂")){
				YOrder r=orderDao.findYOrder(orderNumber);
				list=findSP(r.getServicePersonal());
			}else if(type.equals("保姆")){
				BOrder r=orderDao.findBOrder(orderNumber);
				list=findSP(r.getServicePersonal());
			}
			 map.put("0", list);
		return map;
	}
	public String updateTaskCancelOrder() {
		List<String> orderNumbers=orderDao.taskCancelOrderOrderNumbers();
		//发送短信
		for(String str:orderNumbers){
			new YunTongXun().register(str,"86574",null);
		}
		int q1=orderDao.rupdateTaskCancelOrder();
		int q2=orderDao.supdateTaskCancelOrder();
		int q3=orderDao.kupdateTaskCancelOrder();
		int q4=orderDao.jupdateTaskCancelOrder();
		int q5=orderDao.dupdateTaskCancelOrder();
		int q6=orderDao.cupdateTaskCancelOrder();
		int q=q1+q2+q3+q4+q5+q6;
		String result="暂无取消订单";
		if(q!=0){
			result="很遗憾,订单跑了";
		}
		return result;
		
	}
	public String updateTaskDefaultEvaluation() {
		int q1=orderDao.rupdateTaskDefaultEvaluation();
		int q2=orderDao.supdateTaskDefaultEvaluation();
		int q3=orderDao.kupdateTaskDefaultEvaluation();
		int q4=orderDao.jupdateTaskDefaultEvaluation();
		int q5=orderDao.dupdateTaskDefaultEvaluation();
		int q6=orderDao.cupdateTaskDefaultEvaluation();
		int q=q1+q2+q3+q4+q5+q6;
		String result="暂无需要默认评价的订单";
		if(q!=0){
			result="默认评价了订单";
		}
		return result;
		
	}
	public String findDaojishi(String orderNumber, String type) {
		String result="";
		if(type.equals("日常保洁")){
			result=orderDao.getRDaojishi(orderNumber);
		}else if(type.equals("沙发护理")){
			result=orderDao.getSDaojishi(orderNumber);
		}else if(type.equals("开荒保洁")){
			result= orderDao.getKDaojishi(orderNumber);
		}else if(type.equals("家电清洗")){
			result=orderDao.getJDaojishi(orderNumber);
		}else if(type.equals("地板护理")){
			result=orderDao.getDDaojishi(orderNumber);
		}else if(type.equals("擦玻璃")){
			result=orderDao.getCDaojishi(orderNumber);
		}
		return result;
	}

	public static List paixuOrderQuery(List<OrderQuery> listall){
		for (int i = 0; i < listall.size(); i++) {   
	        int k = i;   
	        for (int j = listall.size() - 1; j >i; j--)  {   
	            if (Long.valueOf(listall.get(j).getOrderNumber()) > Long.valueOf(listall.get(k).getOrderNumber()))  k = j;   
	        }   
	        
	        OrderQuery  temp = listall.get(i);   
	        listall.set(i,listall.get(k));   
	        listall.set(k,temp);   
	    }   
		return listall;
		
	}
	
	
	
	
}
